package com.test;

import org.junit.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by Kevin on 2017-8-8.
 */
//@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 
//@ContextConfiguration({"/spring.xml", "/spring-mybatis.xml"}) //加载配置文件 
public class FileUpload {

    @Test
    public void testUpload() throws Exception {
    	try {
			
        String url = "http://120.25.120.99:9999/upload";
        String filePath = "C:\\Users\\Kevin\\Desktop\\five.jpg";

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setBufferRequestBody(false);
        RestTemplate rest = new RestTemplate(requestFactory);
//        RestTemplate rest = new RestTemplate();
//        FileSystemResource resource = new FileSystemResource(new File(filePath));
//        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
//        param.add("file", resource);
//        param.add("fileName", "abc.png");
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();
//			parts.add("uploadfile", new FileSystemResource(uploadfile));//传统方式
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InputStream is = new FileInputStream(filePath);
        byte[] buffer = new byte[1024];
        while (is.read(buffer) > 0) {
            bos.write(buffer);
        }
        is.close();
        bos.close();
        ByteArrayResource arrayResource = new ByteArrayResource(bos.toByteArray()){
            @Override
            public String getFilename() throws IllegalStateException {
                return "five.png";
            }

        };
        param.add("file", arrayResource);


        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param);
//        HttpEntity<byte[]> httpEntity = new HttpEntity<>(readInputStream(new FileInputStream(new File(filePath))), httpHeaders);
        ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println(responseEntity.getBody());

//        System.out.println(string);
    	} catch (Exception e) {
			// TODO: handle exception
		}
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}
